// 实现这个项目的构建任务
const sass = require('sass')

const loadGruntTasks = require('load-grunt-tasks')
// 常用 grunt-contrib-clean
// grunt-contrib-watch
module.exports = grunt => {
  grunt.initConfig({
    // 获取 package.json 的信息
    pkg: grunt.file.readJSON('package.json'),
    sass: {
      options: {
        sourceMap: true,
        implementation: sass
      },
      main: {
        files: {
          'dist/assets/styles/*.scss': 'src/assets/styles/*.scss'
        }
      }
    },
    cssmin: { 
      options:{ 
          report:'gzip'
      },
      build: { 
        files: [{
          expand: true,
          cwd: 'src',
          src: '**/*.scss',
          dest: 'dist'
        }]
      }
    },
    htmlmin: {
      options: {
        removeComments: true, // 去除注释信息 
        collapseWhitespace: true, // 去除空白字符  
        removeEmptyAttributes: true, // 去除标签的空属性
        removeCommentsFromCDATA: true, // 去除 CDATA 的注释信息
        removeRedundantAttributes: true  // 去除标签的冗余属性
      },
      build:{ 
        files: [{
          expand: true,
          cwd: 'src',
          src: '**/*.html',
          dest: 'dist'
        }]
      }
    },
    babel: {
      options: {
        sourceMap: false,
        presets: ['@babel/preset-env']
      },
      dist: {
        // files: {
        //   'src/assets/scripts/*.js': 'src/assets/scripts/*.js'
        // }
        files: [{
          expand: true, //启用动态扩展
          cwd: 'src', //批匹配相对lib目录的src来源
          src: '**/*.js', //实际的匹配模式
          dest: 'dist', //目标路径前缀
        }]
      }
    },
    uglify: {
      options: {
      	//压缩后的文件注释信息
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      // build: {
      // 	//需要压缩的文件
      //   src: 'src/assets/scripts/*.js',
      //   //压缩后的文件
      //   dest: 'dist/assets/scripts/*.js'
      // }
      build: {
        files: [{
          expand: true, //启用动态扩展
          cwd: 'src', //批匹配相对lib目录的src来源
          src: '**/*.js', //实际的匹配模式
          dest: 'dist', //目标路径前缀
          ext: '.min.js' //目标文件路径中文件的扩展名.
        }]
      }
    },
    clean: { // 清空dist文件
      del : ['dist']
    },
    watch: {
      js: {
        files: ['src/assets/scripts/*.js'],
        tasks: ['babel']
      },
      css: {
        files: ['src/assets/styles/*.scss'],
        tasks: ['sass']
      },
      view: {
        files: ['src/**/*']
      }
    }
  })
  //    grunt.loadNpmTasks('grunt-sass')
  loadGruntTasks(grunt)

  grunt.registerTask('default', ['clean', 'sass', 'babel', 'htmlmin', 'cssmin', 'uglify', 'watch'])
}

// 常用插件

// grunt-contrib-uglify
// grunt-contrib-cssmin
// grunt-contrib-watch
// grunt-cmd-concat
// grunt-contrib-htmlmin
// grunt-contrib-imagemin
// grunt-contrib-clean